brainpopfandomcom-20200223-history
Loops/Transcript
Transcript Text reads: The Mysteries of Life with Nat and Moby Nat, Moby and JazzBot, and Tim are performing a song in an attic. Nat is playing the keyboard, Moby is playing the guitar, and JazzBot is playing the drums. Tim is wearing black clothing and dark eye makeup as he sings into a microphone. Jazzbot suddenly stops playing and jumps on his stool. He points his drumsticks at Nat. JAZZBOT: Bloop! NAT: I was not ahead of the beat! JAZZBOT: Bloop! NAT: Look, Jazzbot, this is rock 'n roll. There's a certain looseness that maybe you're not familiar— JAZZBOT: Bloop! JazzBot knocks over the drum set and jumps off his stool. He rolls out of the room and slams the door behind him. Moby frowns MOBY: Beep! NAT: Whatever, that's the third time he's quit this year. He'll be back. In the meantime, we can replace him with this beat-making program. Nat holds up a tablet with the program displayed on the screen. An e-mail notification appears. NAT: Hey, a letter! Nat reads from the e-mail. NAT: Dear Nat and Moby, My teacher said loops are useful in coding. What are loops? Sincerely, Missy E. Hey Missy! Your teacher is right. A loop is an action that gets repeated over and over. Moby walks over to a chair and spins around in circles. MOBY: Beep? NAT: Yup, loops are found all over the place. A race around a track, a newspaper route, your daily class schedule. An animation shows people running around a track, a GPS map of a neighbor on someone's cell phone, and a class schedule. NAT: Like, I've got an even-odd system, so one day, I'll have periods 1, 3, 5, and 7. The next day, I'll have periods 2, 4, 6, and 8. Then back to the odds, and repeat, on and on for the whole school year. Nat holds her schedule, with her class periods separated into "Day A" and "Day B" columns. NAT: Sometimes I feel like my whole life is stuck in a loop! MOBY: Beep? NAT: Right, loops are especially important in programming! Because a computer often has to perform the same action over and over…Hundreds, thousands, even millions or billions of times in a row! And unlike us humans, they don't get tired or bored of doing it. An animation shows a factory full of robot arms welding metal parts on a conveyor belt. They all make the same movements. NAT: A digital clock is a good example. It keeps adding one second to the time, every second. After 60 seconds, it adds a minute; and after 60 minutes, it adds an hour. Around and around, forever and ever into infinity… or until you turn it off. An animation shows a digital clock on a bedside table. The time changes in the way Nat describes. Then, the animation speeds up, showing the passing of days. Eventually, the animation changes to show a future landscape. The walls of the room have crumbled and vines are growing around it. The time on the clock continues to change. NAT: So, it's an infinite loop. And that's okay, because a clock has one job: to keep on counting. A robot version of Tim walks into the room and hits the clock, turning it off. NAT: But most programs do more than one thing. An animation shows an anthropomorphic cell phone, with arms, legs, and a face. It presses a text icon on its screen and a speech bubble reading "Hey there" appears. NAT: So if an infinite loop occurs, it's usually a mistake in the code. The loop uses up more and more of the available memory. Which eventually causes the program, or the whole computer, to crash. An infinity symbol appears on the phone's screen. It presses the text icon repeatedly, with identical images of the phone and "Hey there" speech bubble multiplying until they fill the screen. Eventually, the original phone begins to smoke and it collapses. The multiplied speech bubbles appear overhead. NAT: Usually, you want to repeat an action only for a set number of times. Like when you get a call on your phone. You want it to ring more than once, so you have a chance to pick up. But it can't just keep ringing if you're not around or just too busy to talk. After a certain number of rings, it needs to stop, and go to voicemail. The anthropomorphic phone sits on a couch watching TV. It holds a remote in one hand and eats popcorn with his other. As he rings, he grows more and more annoyed, turning up the volume of the TV. He finally press the voicemail icon on his screen, stopping the ringing. NAT: In other words, the ringing is programmed as a finite loop: an action that repeats a specific number of times. MOBY: Beep? NAT: Well, there are lots of different ways to program loops. It really depends on the language and platform you're using. Take our beatmaker here. Each instrument gets a row, and the columns tell you their spot in the sequence. An animation shows a close-up of Nat's tablet screen. The beatmaker program displays various instruments in rows and columns representing time measures. NAT: How about… we lay down a little hi-hat pulse…And throw a couple snare and bass drum hits in there. Now, let's listen. Nat presses buttons on the screen and the music plays. Moby crosses his arms. MOBY: Beep. NAT: I know, I know…But that's just a single measure. If we loop it, say, 10 times... Nat enters the number 10 and presses the program's loop button. The music plays, repeating the measure over and over. NAT: Voila—we’ve got a sick beat! MOBY: Beep? NAT: Right, all of these bars and columns represent lines of code. We can see what it looks like here. Nat changes the program's display so that it shows lines of code. NAT: But… let's delete all that and try to write the code ourselves. To play a hi-hat sound, we’ll write "play," followed by "hi-hat" in parentheses. Then three more times, since it plays more than once in this beat. Then add in the bass and snare. The animation shows the tablet screen as Nat enters the lines of code she describes. The final code reads: play(hi-hat) play(bass) play(hi-hat) play(hi-hat) play(snare) play(hi-hat) NAT: Now let's play that sucker! Nat presses "play." All of the instruments play once at the same time, making an unpleasant sound. Moby covers his ears. NAT: Oops, we didn't tell it when to play all these sounds. MOBY: Beep? NAT: Well, you have to remember, computers don't know drums from driftwood. Or anything from driftwood, for that matter. An anthropomorphic tablet holds drumsticks and looks at a drum and a piece of driftwood laying in front of him. NAT: That's why when you're coding, you’ve got to write out each detail. Computers won't do anything unless you tell them to do it. And you have to be specific. The animation shows Moby lifting the tablet and placing it behind the drum. It looks confused and still doesn't play the drums. Moby throws up his hands in frustration. NAT: Here, I made the mistake of just listing out the different sounds to play. We need to tell the drummer to wait before playing each next beat. So, hi-hat and bass at the same time, then wait a bit...Then the next beat, and wait, and so on! The animation shows Nat's finger pointing at the lines of code. She inserts "wait(1)" into three lines of the code. NAT: Okay, let's try again. Nat presses "play" and the beat plays correctly. NAT: Now, to loop it, we add a repeat command plus brackets around everything we want to repeat. Nat adds "repeat" to the first line of code and places brackets around the rest of the lines of code. Her finger hovers over the "play" button when Moby points to the first line of code. MOBY: Beep! NAT: Ooh, right, unless we tell it how many times to repeat, it'll just keep going! We'll want to make it a finite loop! Let's tell this thing to repeat the sequence, uh, 10 times. Nat adds "10" to the first line of code so that it reads "repeat 10{" and presses "play." A screen on Moby's front panel keeps count as the beat repeats 10 times. NAT: And there it is! MOBY: Beep? Moby points to the code. NAT: All those little details, like brackets and parentheses, are called syntax. Different programming languages use different syntax to send the same message. Sometimes the differences between languages are small. Like how we say "hello" in English, and in German it's "hallo." The animation shows an American woman next to a speech bubble reading "Hello!" Another image shows a German man next to a speech bubble reading "Hallo!" NAT: Other times the differences are bigger, like how in Japanese, "hello" is "konichiwa." An animation shows a Japanese woman next to a speech bubble reading "Konichiwa!" NAT: One popular programming language is JavaScript, which looks a lot like our beatmaker code here. As image shows the beatmaker and Javascript codes side-by-side. They both are text-based code, with the repeat function listed first, followed by the code for each instrument. Both use brackets and parentheses. NAT: In a block-based language, like Scratch, it would look something like this. The animation adds the lines of Scratch code. Each command is written on its own block, with the commands for the instruments nestled under a "repeat 10" block. NAT: Notice how each language uses "repeat" to start a loop… The animation enlarges the first line of each code. The beatmaker code reads repeat 10 and the JavaScript code reads repeat (async function. The Scratch code reads repeat 10. NAT:…But they all have a different way of saying "play snare"… The animation enlarges the snare drum line of each code. The beatmaker code reads play(snare) and the JavaScript code reads audio("snare drum"); The Scratch code reads play sound snare drum. NAT: …And "wait a tenth of a second," and "play bass?" The lines of code Nat references all vary in phrasing. NAT: The syntax changes, but the looping concept stays the same: repeating some set of actions, usually for a limited number of times. Nat hovers each line of code and, when she does, the beat plays. It sounds exactly the same as she hovers over the beatmaker, JavaScript, and Scratch code. Soft music begins to play in the attic. MOBY: Beep? NAT: Do you hear it, too? Moby and Nat go to the window and look outside. JazzBot is standing on the lawn, holding a giant boombox over his head. Nat and Moby run out the front door and hug JazzBot. NAT: Let's never fight again. JAZZBOT: Bloop! The animation changes to show Tim looking out the attic window at the scene. He places one hand on the glass and closes his eyes. Category:BrainPOP Transcripts Category:BrainPOP Engineering & Technology Transcripts